
import os,time,random
import pyautogui
import pyperclip

from io import BytesIO
from PIL import Image
# import win32clipboard as clb
# import pywin32clipboard 
# from win32 import win32clipboard as clb

from wechatdata import data_prase as wdp

# import win32gui

BASE_DIR = os.path.dirname(os.path.abspath(__file__))


def search_by_name(name) :
    pyautogui.hotkey('ctrl', 'f')
    pyperclip.copy(name)
    pyautogui.hotkey('ctrl', 'v')
    time.sleep(0.5)     # 要有0.5s才反应正常
    pyautogui.press('enter')

def send_message(msg) :
    pyperclip.copy(msg)
    pyautogui.hotkey('ctrl', 'v')
    pyautogui.press('enter')

def send_msg_list(msglist) :
    for onemsg in msglist:
        print(onemsg)
        time.sleep(random.uniform(0.2, 1))
        send_message(onemsg)

def send_text_message(name, msglist) :
    search_by_name(name)
    send_msg_list(msglist)
    time.sleep(0.3)

def copy_image_to_clip(imagepath) :
    if not os.path.exists(imagepath) :
        return
    pyperclip.copy(imagepath)
    pyautogui.hotkey('win', 'r')
    time.sleep(0.1)
    pyautogui.hotkey('ctrl', 'v')
    time.sleep(0.1)
    pyautogui.press('enter')
    time.sleep(3)
    pyautogui.hotkey('ctrl', 'c')
    pyautogui.hotkey('alt', 'f4')

def send_image_message(name, imagepath) :
    if not os.path.exists(imagepath) :
        return
    search_by_name(name)
    # time.sleep(5)
    copy_image_to_clip(imagepath)
    pyautogui.hotkey('ctrl', 'alt', 'w')
    time.sleep(1)
    pyautogui.hotkey('ctrl', 'v')
    time.sleep(random.uniform(0.5, 2))
    pyautogui.press('enter')
    
# def send_image_message(imagepath) :
#     # image = Image.open(imagepath)
#     # # 声明output字节对象
#     # output = BytesIO()
#     # # 用BMP (Bitmap) 格式存储
#     # # 这里是位图，然后用output字节对象来存储
#     # image.save(output, 'BMP')
#     # # BMP图片有14字节的header，需要额外去除
#     # data = output.getvalue()[14:]
#     # # 关闭
#     # output.close()
#     # pyperclip.copy(data)
#     # clb.OpenClipboard()
#     # clb.EmptyClipboard()
#     # clb.SetClipboardData(clb.CF_DIB, data)
#     # clb.CloseClipboard()
#     pyautogui.hotkey('ctrl', 'v')
#     time.sleep(random.uniform(0.5, 2))
#     pyautogui.press('enter')

def send_at_by_hotkey(name) :
    pyautogui.hotkey('shift', '2')
    pyperclip.copy(name)
    pyautogui.hotkey('ctrl', 'v')
    time.sleep(0.1)


def send_to_group(group, msglist, atstr) :
    search_by_name(group)
    # send_msg_list(msglist)
    send_image_message('123.png')
    send_message('/::~')
    atlist = atstr.split('@')
    atlist.remove('')
    print(atlist)
    for atone in atlist:
        # "".replace('\u2005', '')
        print(atone)
        print(len(atone))
        send_at_by_hotkey(atone)
    time.sleep(0.3)
    # pyautogui.press('enter')


def main():
    configdata = wdp.read_data_from_csv('wedata.csv')
    optlen = len(configdata)
    print(optlen)
    pyautogui.hotkey('ctrl', 'alt', 'w')
    for one in configdata:
        name, (txttype, txt), (imgtype, img), (attype, atone) = one
        print(name)
        if txttype != True:
            print(txt)
            send_text_message(name, txt)
        if imgtype != True:
            print(img)
            send_image_message(name, img)
        if attype != True:
            print(atone)
            # send_image_message('123.png')

if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        os._exit(1)

